package com.ye.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.ye.config.CustomUrlBlockHandler;
import com.ye.pojo.Student;
import com.ye.util.ExceptionUtil;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @author: scott
 * @date: 2022年04月06日 16:25
 */
@RestController
@RequestMapping("/discovery")
public class DiscoveryController {
    @NacosInjected
    private NamingService namingService;

    @GetMapping("/instances")
    public List<Instance> discovery(@RequestParam String serviceName) throws NacosException {
        return namingService.getAllInstances(serviceName);
    }

    @GetMapping("/register")
    public void register() throws NacosException {
        namingService.registerInstance("dis-discovery", "127.0.0.1", 20002);
    }

    @GetMapping("/select")
    public String selectService() throws NacosException {
        Instance instance = namingService.selectOneHealthyInstance("SPRING-BOOT-SERVICE-CONFIG-PROVIDER", "DEFAULT_GROUP");
        String ip = instance.getIp();
        int port = instance.getPort();
        RestTemplate restTemplate = new RestTemplate();
        String url = String.format("http://%s:%d/config/teacher", ip, port);
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
        return responseEntity.getBody();
    }

    @GetMapping(value = "/json", consumes = MediaType.APPLICATION_JSON_VALUE)
    @SentinelResource(value = "json", blockHandler = "jsonHandleException")
    public String selectStudent(@RequestBody Student student) {
        return JSONObject.toJSONString(student);
    }

    public String jsonHandleException(@RequestBody Student student, BlockException ex) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("param", student);
        jsonObject.put("message", ex.getMessage());
        jsonObject.put("exception", ex.getClass().getCanonicalName());
        return JSONObject.toJSONString(jsonObject);
    }

    @GetMapping(value = "/pojo")
    @SentinelResource(value = "pojo", blockHandler = "pojoHandleException",blockHandlerClass = ExceptionUtil.class)
    public String selectPojo(HttpServletRequest request) {
        Map<String, String[]> parameterMap = request.getParameterMap();
        JSONObject jsonObject = new JSONObject();
        for (String s : parameterMap.keySet()) {
            if (parameterMap.get(s).length == 1) {
                jsonObject.put(s, parameterMap.get(s)[0]);
            } else {
                jsonObject.put(s, parameterMap.get(s));
            }
        }
        return jsonObject.toJSONString();
    }

    @GetMapping("/rule")
    public String getStudent(HttpServletRequest request) {
        int port = request.getServerPort();
        String servletPath = request.getServletPath();
        String serverName = request.getServerName();
        String api = serverName + ":" + port + servletPath;
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("API", api);
        return JSONObject.toJSONString(jsonObject);
    }
}
